/**
 * 
 */
package co.edu.udem.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import co.edu.udem.dao.SubjectDao;
import co.edu.udem.domain.Subject;
import co.edu.udem.domain.UserSubject;
import co.edu.udem.service.login.SessionManager;

/**
 * @since 29/07/2012
 */
@Repository("subjectDao")
@Transactional
public class SubjectDaoImpl implements SubjectDao {

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/** Instance of session. */
	@Autowired
	private SessionManager sessionManager;

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.edu.udem.dao.SubjectDao#getObjectList(java.util.Date,
	 * java.lang.String[][], java.lang.Object[])
	 */
	public List<Object[]> getObjectList(Date serviceDate, String[][] orderBy,
			Object... params) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("FROM  Subject sub, UserSubject us Where userCode = :userCode And userType = :userType  And us.subjectCode = sub.subjectCode");
		query.setParameter("userCode", params[0]);
		query.setParameter("userType", new Integer ((String)params[1]));
		return (List<Object[]>) query.list();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * co.edu.udem.dao.SubjectDao#getSubjectListByTeacherCode(java.lang.String)
	 */
	public List<Subject> getSubjectListByTeacherCode(String teacherCode) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("FROM  Subject where teacherCode = :teacherCode");
		query.setParameter("teacherCode", teacherCode);
		return (List<Subject>) query.list();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.edu.udem.dao.SubjectDao#addSubject(co.edu.udem.domain.Subject)
	 */
	public void addSubject(Subject subject) {
		Session session = sessionFactory.getCurrentSession();
		session.merge(subject);
	}

	public UserSubject getSubjectDetailsByUser(String userCode, int userType,
			String subjectCode) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("FROM  UserSubject where subjectCode = :subjectCode And userCode = :userCode And userType = :userType");
		query.setParameter("subjectCode", subjectCode);
		query.setParameter("userCode", subjectCode);
		query.setParameter("userType", subjectCode);
		return (UserSubject) query.uniqueResult();
	}
	
	public Subject getSubjectById(String subjectCode) {
		Session session = sessionFactory.getCurrentSession();
		Subject subject = (Subject) session.get(Subject.class, subjectCode);
		return subject;
	}
	
	public void delete(String subjectCode) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("DELETE FROM SubjectDetail Where subjectCode = :subjectCode");
		query.setParameter("subjectCode", subjectCode);
		query.executeUpdate();
		query = session.createQuery("DELETE FROM UserSubject Where subjectCode = :subjectCode");
		query.setParameter("subjectCode", subjectCode);
		query.executeUpdate();
		Subject subject = (Subject) session.get(Subject.class, subjectCode);
		session.delete(subject);
	}

}
